package com.flute.icrawler.app.monitor;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jdmk.comm.HtmlAdaptorServer;

public class CrawlMonitorService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(CrawlMonitorService.class);

	/**
	 * 注册监控对象
	 * 
	 * @param monitoredEntity
	 *            被注册的监控对象
	 * @throws Exception
	 */
	public static void registerMonitor(CrawlMonitoredEntity monitoredEntity)
			throws Exception {
		MBeanServer server = MBeanServerFactory.createMBeanServer();

		int port = 9999;
		// register HtmlAdaprotServer
		HtmlAdaptorServer adapter = new HtmlAdaptorServer();

		ObjectName adapterName = new ObjectName("Agent:name=htmladapter,port="
				+ port);
		adapter.setPort(port);
		server.registerMBean(adapter, adapterName);

		CrawlMonitor crawlMonitor = new CrawlMonitor(monitoredEntity);
		ObjectName crawlMonitorObject = new ObjectName(
				"Monitor:name=CrawlMonitor");
		// register JobMonitor
		server.registerMBean(crawlMonitor, crawlMonitorObject);
		adapter.start();

		LOGGER.info("CrawlMonitor Server started...");

		LOGGER.info("http://127.0.0.1:" + port);

	}
}
